package com.mike.fragmentdemo3.Fragment.Adpater;

import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.mike.fragmentdemo3.Fragment.Activity.SmartHomeActivity;
import com.mike.fragmentdemo3.Fragment.Fragment.EquipmentFragment;
import com.mike.fragmentdemo3.Fragment.Fragment.PatternFragment;

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private static final int PAGER_COUNT = 2;
    private final EquipmentFragment mEquipmentFragment;
    private final PatternFragment mPatternFragment;

    public MyFragmentPagerAdapter(@NonNull FragmentManager fm) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        mEquipmentFragment = new EquipmentFragment();
        mPatternFragment = new PatternFragment();
    }

    @Override
    public int getCount() {
        return PAGER_COUNT;
    }

    @Override
    @NonNull
    public Fragment getItem(int position) {
        switch (position) {
            case SmartHomeActivity.PAGE_ONE:
                return mEquipmentFragment;
            case SmartHomeActivity.PAGE_TWO:
                return mPatternFragment;
            default:
                throw new IllegalArgumentException("Invalid page position: " + position);
        }
    }

    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        System.out.println("Position Destroy: " + position);
        super.destroyItem(container, position, object);
    }
}
